package com.cndym.service.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cndym.dao.IFillByYYDao;
import com.cndym.service.IFillByYYService;
import com.cndym.utils.DateUtils;
import com.cndym.utils.FillConfigUtils;
import com.cndym.utils.JsonBinder;
import com.cndym.utils.SpringUtils;
import com.cndym.utils.hibernate.PageBean;

/**
 * @author faxc
 * @date 2013-7-22 下午05:39:50
 */
@Service
public class FillByYYServiceImpl implements IFillByYYService {
	private Logger logger = Logger.getLogger(getClass());
	private static final String PRESENT_AMOUNT = "03";
	@Autowired
	IFillByYYDao fillByYYdao;

	@Override
	public boolean doSave(Object o) {

		return false;
	}
	
	@Override
	public List getListBySQL(String sql) {

		return fillByYYdao.getListBySQL(sql);
	}

	@Override
	public Object getTBySQL(String sql, Class c) {

		return fillByYYdao.getTBySQL(sql, c);
	}

	@Override
	public void update(Object o) {
		fillByYYdao.update(o);
	}

	@Override
	public void doupdate(Object o) {
		fillByYYdao.update(o);
	}

	public void updateBySQL(String sql) {
		fillByYYdao.updateBySQL(sql);
	}

	public List jsonArrayToMap(String res) {

		String jsonStr = res;

		try {
			Map<String, Object> paraMap = JsonBinder
					.buildNormalBinder()
					.getMapper()
					.readValue(jsonStr,
							new TypeReference<Map<String, Object>>() {
							});

			List<Map<String, Object>> codeArray = (List<Map<String, Object>>) paraMap
					.get("codeArray");
			return codeArray;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	@Override
	public PageBean getPageBean(String select, Object[] paramValues,
			int pageId, int pageSize) {

		return fillByYYdao.getPageBean(select, paramValues, pageId, pageSize);
	}

	@Override
	public void updateBySQL(String sql, String[] param) {
		fillByYYdao.updateBySQL(sql, param);
	}

	@Override
	public void saveObject(Object o) {
		fillByYYdao.save(o);
	}

	@Override
	public void saveAllObject(List l) {

		fillByYYdao.saveAllObject(l);

	}

	@Override
	public List getListBySQL(String sql, Object[] param) {

		return fillByYYdao.getListBySQL(sql, param);
	}

	public static void main(String args[]) {
		String PRESENT_WEIXIN_EXP = FillConfigUtils
				.getValue("PRESENT_WEIXIN_EXP");// 有效期以逗号(,)隔开
		String PRESENT_ALIPAY_EXP_arr[] = PRESENT_WEIXIN_EXP.split(",");
		String dateString = DateUtils.formatDate2Str(new Date(), "yyyy-MM-dd");

		;
		dateString = DateUtils.formatDate2Str(
				DateUtils.formatStr2Date("2014-09-18", "yyyy-MM-dd"),
				"yyyy-MM-dd");
		System.out
				.println(DateUtils.formatStr2Date("2014-09-18", "yyyy-MM-dd"));
		if (dateString.compareTo(PRESENT_ALIPAY_EXP_arr[0]) >= 0
				&& dateString.compareTo(PRESENT_ALIPAY_EXP_arr[1]) <= 0) {
			System.out.println("in the days");
		} else {
			System.out.println("not in the days");
		}

		// readFileByLines("F:/xad.sql");
		// readFileByLines("F:/test.txt");

	}

	public static void readFileByLines(String fileName) {
		IFillByYYService fff = (IFillByYYService) SpringUtils
				.getBean("fillByYYServiceImpl");
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			System.out.println("以行为单位读取文件内容，一次读一行");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			// 一次读一行，读入null时文件结束
			while ((tempString = reader.readLine()) != null) {
				// 把当前行号显示出来
				fff.updateBySQL(tempString.replace(";", ""));
				System.out.println("line " + line + ": " + tempString);

				line++;

			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	public List getListWithTypeBySQL(String sql, Object[] param, Class c) {
		return fillByYYdao.getListWithTypeBySQL(sql, param, c);
	}
}
